package com.aloha.springframework.beans.factory.config;

import com.aloha.springframework.beans.PropertyValues;

import java.util.Objects;

/**
 * @author DaiZhiHeng
 * @description Bean定义信息，封装了创建对象所需的类型、属性等信息
 * @date 2023/6/27 20:20
 */

public class BeanDefinition {

    private static final String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;

    private static final String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;

    /**
     * 被BeanDefinition修饰的对象的实际类型
     */
    private Class<?> beanClass;

    /**
     * 实例化时的属性值
     */
    private PropertyValues propertyValues;

    /**
     * 初始化方法的名称
     */
    private String initMethodName;

    /**
     * 销毁方法的名称
     */
    private String destroyMethodName;

    /**
     * 这类型对象的是使用单例模式还是原型模式
     */
    private String scope = SCOPE_SINGLETON;

    /**
     * 默认都是单例模式
     */
    private boolean singleton = true;

    private boolean prototype = false;

    /**
     * bean是否懒加载
     */
    private boolean lazyInit = false;

    public BeanDefinition(Class<?> beanClass) {
        this(beanClass, null);
    }

    public BeanDefinition(Class<?> beanClass, PropertyValues propertyValues) {
        this.beanClass = beanClass;
        this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
    }

    public void setScope(String scope) {
        this.scope = scope;
        this.singleton = SCOPE_SINGLETON.equals(scope);
        this.prototype = SCOPE_PROTOTYPE.equals(scope);
    }

    public boolean isSingleton() {
        return singleton;
    }

    public boolean isPrototype() {
        return prototype;
    }

    public Class<?> getBeanClass() {
        return beanClass;
    }

    public void setBeanClass(Class<?> beanClass) {
        this.beanClass = beanClass;
    }

    public PropertyValues getPropertyValues() {
        return propertyValues;
    }

    public void setPropertyValues(PropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }

    public String getInitMethodName() {
        return initMethodName;
    }

    public void setInitMethodName(String initMethodName) {
        this.initMethodName = initMethodName;
    }

    public String getDestroyMethodName() {
        return destroyMethodName;
    }

    public void setDestroyMethodName(String destroyMethodName) {
        this.destroyMethodName = destroyMethodName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BeanDefinition that = (BeanDefinition) o;
        return beanClass.equals(that.beanClass);
    }

    @Override
    public int hashCode() {
        return Objects.hash(beanClass);
    }

    public void setLazyInit(boolean b) {
        lazyInit = b;
    }

    public boolean isLazyInit() {
        return lazyInit;
    }

}
